home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #2 / Amiga Plus CD - 2004 - No. 02.iso / AmigaPlus / Tools / Development / AmigaTalk / user / UserGUI.st < prev    next >
Text File  |  2004-01-31  |  6KB  |  200 lines

  1. " --------------------------------------------------------------------
  2. * The UserGUI Class allows the User to interface with
  3. * a view of some other Objects (Usually Data Entry Gadgets).
  4. * The User is the only entity that can change the data in the display,
  5. * hence the User serves as the Model UserGUI is a combination of the
  6. * View & Controller of the MVC triad.
  7. * --------------------------------------------------------------------
  8. "
  9. Class UserGUI :Object ! guiWindow guiScreen ctrlDictionary !
  10. [
  11.    controls
  12.    
  13.       ^ ctrlDictionary
  14. |
  15.    controlAt: index
  16.    
  17.       ^ ctrlDictionary at: index
  18. |
  19.    new
  20.  
  21.       ctrlDictionary <- Dictionary new.
  22.       
  23.       ^ self
  24. |
  25.    setScreen: newScreen
  26.  
  27.       guiScreen <- newScreen
  28. |       
  29.    screen
  30.  
  31.       ^ guiScreen
  32. |
  33.    setWindow: newWindow
  34.  
  35.       guiWindow <- newWindow       
  36. |
  37.    window
  38.  
  39.       ^ guiWindow
  40. |
  41.    textAttributes
  42.  
  43.       ^ guiScreen getFontObject
  44. |
  45.    visualInfo
  46.  
  47.       ^ guiScreen getVisualInfo: nil
  48. |
  49.    startUp 
  50.  
  51.       " Give control to the receiver.  The default control sequence is 
  52.       * to initialize (see UserGUI/controlInitialize), to loop (see 
  53.       * UserGUI/controlLoop), and then to terminate (see 
  54.       * UserGUI/controlTerminate).  After this sequence, control is 
  55.       * returned to the sender of UserGUI/startUp.  The receiver's control 
  56.       * sequence is used to coordinate the interaction of the User with
  57.       * the underlying data contained in the GUI.
  58.       "
  59.       self controlInitialize.
  60.  
  61.       self controlLoop.
  62.  
  63.       self controlTerminate.
  64. |
  65.    terminateAndInitializeAround: aBlock 
  66.  
  67.       self controlTerminate.
  68.  
  69.       aBlock value. 
  70.  
  71.       self controlInitialize.
  72. |
  73.    controlInitialize ! ele ctrlObject !
  74.  
  75.       " Sent by 'startUp' as part of the standard control sequence, it 
  76.       * provides a place in the standard control sequence for 
  77.       * initializing the receiver (taking into account the current
  78.       * state of its data).
  79.       "
  80.       ^ nil
  81. |
  82.    controlLoop ! notDone userData ! 
  83.  
  84.       " Sent by startUp as part of the standard control sequence. 
  85.       * As long as true is returned, the loop continues. 
  86.       * When false is returned, the loop ends.
  87.       "
  88.       notDone <- true.
  89.       
  90.       [notDone ~= false]
  91.          whileTrue: [ userData <- self waitForUserData.
  92.                       
  93.                       (userData isNil)
  94.                          ifFalse: [ notDone <- self decode: userData ] 
  95.                     ].
  96.       ^ false
  97. |
  98.    controlTerminate ! ele controlObj !
  99.       
  100.       " User did something to exit the UserGUI Loop.
  101.       * Provide a place in the standard control sequence for 
  102.       * terminating the receiver (taking into account the 
  103.       * current state of its data).
  104.       "
  105.       ^ nil
  106. |
  107.    addControl: controlObject named: ctrlID 
  108.    
  109.       ctrlDictionary at: ctrlID put: controlObject.
  110.  
  111.       controlObject registerTo: guiWindow.
  112.  
  113.       controlObject perform: #initializeControl.
  114. |
  115.    addHotKey: keyValue to: controlObject 
  116.  
  117.       " This method is only needed if you decide to change a Gadget hotKey
  118.       * value.  If you specify a valid hotKey when the Gadget is created,
  119.       * you do NOT have to use this method.
  120.       "   
  121.       <primitive 239 3 12 keyValue controlObject>.
  122. |
  123.    addMenuSelection: menuObject named: menuID 
  124.  
  125.       ctrlDictionary at: menuID put: menuObject.
  126.       
  127.       menuObject registerTo: guiWindow.
  128. |
  129.    addMenuHotKey: keyValue to: menuObject " NOT really needed. "
  130.  
  131.       <primitive 239 1 12 keyValue menuObject>.
  132. |
  133.    waitForUserData 
  134.  
  135.       " userData is an Array of five elements, first is the 
  136.       * Gadget Type, second is the Gadget ID, third is the userData 
  137.       * (a #methodSymbol for this Class), fourth is the hotKey value,
  138.       * fifth is the Gadget value.
  139.       * 
  140.       * If the User selected a menu item, the first element is
  141.       * the menu Type, second is the menuitem Label, 
  142.       * third is the menu Symbol fourth is the Command Key equivalent
  143.       * (a through z, 0 through 9, or A through Z only [for now!]).  
  144.       *
  145.       * The AmigaOS will wait at the IDCMP port of the controller
  146.       * window until the User initiates an IDCMP event. 
  147.       "
  148.       ^ <primitive 239 3 9 guiWindow>
  149. |
  150.    closeGUIWindow 
  151.      
  152.       ^ false " User should close & dispose of this (for now). "
  153. |
  154.    rawKey: keyCode  "SubClasses should re-define this method "
  155.  
  156.       " Example:  This will determine what key each keyCode is attached to:
  157.       *    ('You pressed: ', keyCode asString, ' (keyCode)') print
  158.       "  
  159.       ^ true
  160. |
  161.    vanillaKey: keyCode  "SubClasses should re-define this method "
  162.  
  163.       " Vanilla keys are alpha-numeric keys only! "
  164.  
  165.       " Example: 
  166.       *   (keyCode = 'q' or: [keyCode = 'Q'])  Quit KeyPress found?
  167.       *      ifTrue: [ ^ false ]               Which will tell controlLoop to exit
  168.       "
  169.       ^ true
  170. |
  171.    decode: controlData ! ctrlID ctrlSymbol hotKey !
  172.    
  173.       ctrlID     <- controlData at: 2. " Integer, String or nil "
  174.       ctrlSymbol <- controlData at: 3. " Symbol (please!)       " 
  175.       hotKey     <- controlData at: 4. " Character or nil       "
  176.  
  177.       (ctrlSymbol = #closeWindow)
  178.          ifTrue: [ ^ self closeGUIWindow ].
  179.  
  180.       (ctrlSymbol = #rawKey)
  181.          ifTrue: [ ^ self rawKey: hotKey ].
  182.  
  183.       (ctrlSymbol = #vanillaKey)
  184.          ifTrue: [ ^ self vanillaKey: hotKey ].
  185.  
  186.       " IDCMP_MenuPick, IDCMP_GadgetUp & IDCMP_GadgetDown: "  
  187.  
  188.       ^ (ctrlDictionary at: ctrlID) perform: ctrlSymbol. 
  189.    testNewMenuUserData ! rval !
  190.    
  191.       rval <- self waitForUserData.
  192.       
  193.       ('menu userData[1] is: ', (rval at: 1) asString) print.
  194.       ('menu userData[2] is: ', (rval at: 2) asString) print.
  195.       ('menu userData[3] is: ', (rval at: 3) asString) print.
  196.       ('menu userData[4] is: ', (rval at: 4) asString) print.
  197.       ('menu userData[5] is: ', (rval at: 5) asString) print.
  198. ]
  199.